/*
 * Equipe: Menandro Dias E. Neto
 *         Thiago Torres
 *         Ubirajara Gabriela
 */

package com.mapdelivery.model.dao;

import com.mapdelivery.beans.MapDeliveryBean;
import com.mapdelivery.beans.Categoria;
import java.util.ArrayList;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author menandro.neto
 */
public class DAOCategoria implements MapDeliveryDAO {

    public void delete(Integer pk) {
        Database db = new Database();
        String sql = "delete from Categoria where CategoriaId = " + pk.toString();
        db.execSQL(sql);
        db.disConnect();
    }

    public void insert(MapDeliveryBean mdBean) {
        Categoria bean = (Categoria) mdBean;
        Database db = new Database();
        String sql = "insert into Categoria values ("
                   + bean.getCategoriaId().toString() + ", '"
                   + bean.getCatDescricao()           + "') ";
        db.execSQL(sql);
        db.disConnect();
    }

    public void update(Integer pk, MapDeliveryBean mdBean) {
        Categoria bean = (Categoria) mdBean;
        Database db = new Database();
        String sql = "update Categoria set "
                   + "catDescricao = '" + bean.getCatDescricao() + "' "
                   + "where CategoriaId = " + bean.getCategoriaId().toString();
        db.execSQL(sql);
        db.disConnect();
    }

    public ArrayList<MapDeliveryBean> findAll() {
        ResultSet rsBean;
        Categoria bean = null;
        ArrayList<MapDeliveryBean> ret = new ArrayList<MapDeliveryBean>();
        Database db = new Database();
        String sql = "select * from Categoria";
        rsBean = db.getResultSet(sql);
        try {
            while (rsBean.next())
                bean = new Categoria();
                bean.setCategoriaId(rsBean.getInt("CategoriaId"));
                bean.setCatDescricao(rsBean.getString("catDescricao"));
                ret.add(bean);
        }
        catch (SQLException e) {

        }
        db.disConnect();
        return ret;
    }

    public ArrayList<MapDeliveryBean> findByDesc(String arg) {
        ResultSet rsBean;
        Categoria bean = null;
        ArrayList<MapDeliveryBean> ret = new ArrayList<MapDeliveryBean>();
        Database db = new Database();
        String sql = "select * from Categoria where catDescricao = " + arg;
        rsBean = db.getResultSet(sql);
        try {
            while (rsBean.next())
                bean = new Categoria();
                bean.setCategoriaId(rsBean.getInt("CategoriaId"));
                bean.setCatDescricao(rsBean.getString("catDescricao"));
                ret.add(bean);
        }
        catch (SQLException e) {

        }
        db.disConnect();
        return ret;
    }

    public MapDeliveryBean findByPrimaryKey(Integer pk) {
        ResultSet rsBean;
        Categoria bean = null;
        Database db = new Database();
        String sql = "select * from Categoria where CategoriaId = " + pk.toString();
        rsBean = db.getResultSet(sql);
        try {
            while (rsBean.next())
                bean = new Categoria();
                bean.setCategoriaId(rsBean.getInt("CategoriaId"));
                bean.setCatDescricao(rsBean.getString("catDescricao"));
        }
        catch (SQLException e) {

        }
        db.disConnect();
        return bean;
    }

}
